/*
 * @Author: your name
 * @Date: 2022-02-23 18:23:24
 * @LastEditTime: 2022-02-25 15:02:43
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /aone_flutter/lib/packages/e01/views/home/home_page.dart
 */
import 'package:aone_common/common.dart';
import 'package:aone_common/entities/entities.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:prototype/components/v_carousel/v_carousel.dart';
import 'package:prototype/components/v_marquee/v_marquee.dart';
import 'package:prototype/components/v_navigation_bar/v_navigation_bar.dart';
import 'package:prototype/theme/custom_theme.dart';
import 'package:prototype/views/main/main_controller.dart';

class HomeView extends StatelessWidget {
  const HomeView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final services = GlobalService.to;
    final globalState = GlobalService.to.state;
    final scaffoldKey = MainController.to.scaffoldKey;

    return Scaffold(
      backgroundColor: CustomTheme.colorSchema.tertiary,
      appBar: AppBar(
        centerTitle: true,
        leading: IconButton(
          onPressed: scaffoldKey.currentState?.openDrawer,
          icon: const Icon(Icons.menu),
        ),
        title: CachedNetworkImage(
          imageUrl: globalState.siteItems.logoSquare!,
          fit: BoxFit.fill,
          height: 40,
        ),
      ),
      body: SingleChildScrollView(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              const VMarquee(),
              SizedBox(
                height: 10.h,
              ),
              const VCarousel(),
              _shortcutRow(),
              ..._gameList(globalState.navConfig),
            ],
          ),
        ),
      ),
    );
  }

  List<Widget> _gameList(List<Navconfig> nav) {
    final staticUrl = GlobalService.to.state.siteHeaders.necStaticUrl;
    final cleanList =
        nav.where((element) => element.subChildren!.isNotEmpty).toList();
    return cleanList
        .map((parent) => Padding(
              padding: EdgeInsets.only(top: 15.h),
              child: Column(
                children: [
                  Center(
                    child: Image.asset(
                      'assets/images/home/${parent.isYs}TitleLabel.png',
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 20.h),
                    height: 150.h,
                    child: ListView.builder(
                      scrollDirection: Axis.horizontal,
                      itemCount: parent.subChildren!.length,
                      itemBuilder: (BuildContext context, int index) {
                        var e = parent.subChildren![index];
                        return Padding(
                          padding: EdgeInsets.only(
                              right: 20.w, left: index == 0 ? 10.w : 0),
                          child: GestureDetector(
                            onTap: () => onBeforeGameInit(
                              onGameParams: e,
                            ),
                            child: CachedNetworkImage(
                              height: 150.h,
                              width: 130.w,
                              fit: BoxFit.fill,
                              imageUrl: e.image!.contains('http')
                                  ? e.image!
                                  : staticUrl! + e.image!,
                            ),
                          ),
                        );
                      },
                    ),
                  ),
                ],
              ),
            ))
        .toList();
  }

  Widget _shortcutRow() {
    return Container(
      height: 150.h,
      width: 1.sw,
      alignment: Alignment.center,
      padding: EdgeInsets.symmetric(horizontal: 30.w),
      decoration: const BoxDecoration(
        image: DecorationImage(
          fit: BoxFit.fill,
          image: AssetImage(
            'assets/images/home/shortcutBg.png',
          ),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          'deposit',
          'withdraw',
          'message',
          'vip',
        ].map((e) => _shortcut(e)).toList(),
      ),
    );
  }

  Widget _shortcut(String name) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Image.asset('assets/images/home/${name}Icon.png'),
        Text(
          name.tr,
          textHeightBehavior: const TextHeightBehavior(
              leadingDistribution: TextLeadingDistribution.proportional),
          style: TextStyle(fontSize: 14.sp, height: 1.7),
        ),
      ],
    );
  }
}
